package org.wappumobileapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import dataobjects.Category;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * reference:
 * - http://w2davids.wordpress.com/android-listview-with-iconsimages-and-sharks-with-lasers/
 * - http://mobile.tutsplus.com/tutorials/android/android-listview/
 * - http://udinic.wordpress.com/2011/07/01/selectablelistview-make-selection-work/
 * - http://www.vogella.de/articles/AndroidListView/article.html
 * @author Minna_2
 *
 */
public class CategoryArrayAdapter extends ArrayAdapter<Category>{

	private static final String tag = "CategoryArrayAdapter";
	//private static final String ASSETS_DIR = "images/";
	
	private Context context;
	private List<Category> categories = new ArrayList<Category>();
	
    private ImageView categoryIcon;
    private TextView categoryName;
	
    private int mCurrSelected = 0; // Keeping the currently selected item    
    
	public CategoryArrayAdapter(Context context, int textViewResourceId,
			List<Category> objects) {
		super(context, textViewResourceId, objects);
		this.context = context;
		this.categories = objects;
		// Set all categories unselected
		for (Category c : categories) {
			c.setUnselected();
		}
		this.categories.get(mCurrSelected).setSelected(); // set the first category selected.
	}
	
    public int getCount() {
    	return this.categories.size();
	}
	 
	public Category getItem(int index) {
	    	return this.categories.get(index);
	}
	
	public Category getSelectedCategory() {
		return this.categories.get(mCurrSelected);
	} 

	public Category setSelectable(int position) {
		// The -1 value means that no item is selected
		if (mCurrSelected != -1) {
			getItem(mCurrSelected).setUnselected();
		}
			 
		// Selecting the item in the position we got as an argument
		if (position != -1) {
		    getItem(position).setSelected();
			mCurrSelected = position;
		}
			 
		// Making the list redraw
		notifyDataSetChanged();
			 
		return getSelectedCategory();
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			// ROW INFLATION
			Log.d(tag, "Starting XML Row Inflation ... ");
			LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.category_listitem, parent, false);
			Log.d(tag, "Successfully completed XML Row Inflation!");
		}
		
		// Get item
		Category category = getItem(position);
		
		if (category.isSelected()) {
			row.setBackgroundDrawable(this.context.getResources().getDrawable(R.drawable.test_button_background_pressed));//Color(Color.RED);
		} else {
			row.setBackgroundDrawable(this.context.getResources().getDrawable(R.drawable.test_button_background_normal));
			//row.setBackgroundColor(Color.WHITE);
		}
		
		// Set text
		categoryName = (TextView) row.findViewById(R.id.category_name);
		int textResId = this.context.getResources().getIdentifier(category.getTextId(), "string", "org.wappumobileapp");
		categoryName.setText(textResId);
		
		// Set icon
		categoryIcon = (ImageView) row.findViewById(R.id.category_icon);
		int resId = this.context.getResources().getIdentifier(category.getImageId(), "drawable", "org.wappumobileapp");
		categoryIcon.setImageResource(resId);	      
		
		// Set country abbreviation
	    return row;
    }
	
}
